package com.wangsd.web.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.report.entity.Merchant;
import com.wangsd.web.report.entity.ServiceBusiness;
import com.wangsd.web.report.mapper.MerchantMapper;
import com.wangsd.web.report.service.MerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.report.service.ServiceBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-15
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Resource
    MerchantMapper merchantMapper;
    @Autowired
    ServiceBusinessService serviceBusinessService;
    @Override
    public IPage<Merchant> selectList(Integer pageNum, Integer pageSize, Merchant params, String code) {
        Page<Merchant> page = new Page<>(pageNum, pageSize);
        IPage<Merchant> resultIPage = merchantMapper.selectListXml(page, params, code);
        return resultIPage;
    }

    @Override
    public void addOrUpdate(Merchant params) {
        if (params.getId() != null) {
            merchantMapper.updateById(params);
        } else {
            merchantMapper.insert(params);
            //保留检索CODE
            ServiceBusiness serviceBusiness = serviceBusinessService.getById(params.getServiceBusinessId());
            params.setCode(serviceBusiness.getCode() + "-" + params.getId());
            merchantMapper.updateById(params);
        }
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Merchant> list = merchantMapper.selectList(queryWrapper);
        for (Merchant agent : list) {
            Map map = new HashMap();
            map.put("code", agent.getCode());
            map.put("id", agent.getId());
            map.put("name", agent.getName());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<Merchant> selectByBusinessId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("service_business_id", id);
        queryWrapper.eq("deleted", "0");
        List<Merchant> list = merchantMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Merchant selectBySubMchId(String mchId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sub_mch_id", mchId);
        queryWrapper.eq("deleted", "0");
        return merchantMapper.selectOne(queryWrapper);
    }

    @Override
    public Merchant selectBySubMchIdAndNotId(String subMchId, Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sub_mch_id", subMchId);
        if(id != null){
            queryWrapper.ne("id", id);
        }
        queryWrapper.eq("deleted", "0");
        return merchantMapper.selectOne(queryWrapper);
    }
}
